<?php
include_once '../inc/db_util.php';

function viewResources($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    if($jsArray->{'Id'} != NULL){
        $query = mysql_query("SELECT * FROM tb_resources r 
        JOIN tb_det_matkul dm ON r.id_det_matkul = dm.id_det_matkul
        JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
        WHERE r.id_det_matkul='" . $jsArray->{'Id'} . "'");
    } else {
        $query = mysql_query("SELECT * FROM tb_resources r 
            JOIN tb_det_matkul dm ON r.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            WHERE r.id_det_matkul IN (
                SELECT id_det_matkul FROM tb_det_matkul WHERE nip = '" . $jsArray->{'Creator'}->{'Nip'} . "'
            ) ORDER BY r.id_det_matkul");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_resources'];
        $row_array['Judul'] = $row['judul_materi'];
        $row_array['Link'] = $row['link'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function viewResourcesMhs($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    
    if ($jsArray->{'DetMatkul'}->{'Id'} != NULL) {
        $query = mysql_query("SELECT * FROM tb_resources t 
            JOIN tb_det_matkul dm ON t.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            WHERE dm.id_det_matkul='" . $jsArray->{'DetMatkul'}->{'Id'} . "'");
    } else {
        $query = mysql_query("SELECT tq.*, dm.*, mm.*
			FROM tb_resources tq
            JOIN tb_det_matkul dm ON tq.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
			WHERE dm.id_det_matkul IN (
                SELECT id_det_matkul FROM tb_perkuliahan WHERE id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
            ) ORDER BY dm.id_det_matkul");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_resources'];
        $row_array['Judul'] = $row['judul_materi'];
        $row_array['Link'] = $row['link'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];


        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function createResource($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_resources(id_det_matkul, judul_materi, link, tanggal_create)
        VALUES (            
        '" . $jsArray->{'DetMatkul'}->{'Id'} . "',
        '" . $jsArray->{'Judul'} . "',
        '" . $jsArray->{'Link'} . "',
        NOW()
        )";

    return mysql_query($sql);
}

function updateResource($json) {
    $jsArray = json_decode($json, false);

    $sql = "UPDATE tb_resources SET 
        judul_materi='" . $jsArray->{'Judul'} . "', link='" . $jsArray->{'Link'} . "'
        WHERE id_resources='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function deleteResource($json) {
    $jsArray = json_decode($json, false);

    $sql = "DELETE FROM tb_resources WHERE id_resources='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}
?>
